<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>控制线条</title>
    <style>
        /*不能在里面设置宽高*/
        canvas{

            border: 1px solid black;
            transition: 2s linear;
        }
    </style>
</head>
<body>
<button id="bt">点击绘画</button>
<hr>

</body>


<script>
    const canvas = document.createElement('canvas');
    document.body.append(canvas)
    canvas.width = 600
    canvas.height = 400
    const ctx = canvas.getContext('2d')

    const geneateCanvas = () => {

        ctx.moveTo(10, 10);
        ctx.lineTo(150, 180);
        ctx.lineTo(200, 70);
        ctx.lineTo(300, 330);

        //设置线条宽度，这是个全局的，后面大小都是20了
        //让线条绿色
        ctx.strokeStyle='green'
        ctx.lineWidth = 10;
        ctx.stroke()

        //暂停2s后重新绘制
        setTimeout(() => {
            //stroke会把所有点重新绘制，所以最后宽度都是20
            ctx.lineTo(200, 350);
            ctx.lineWidth = 20;


            //https://developer.mozilla.org/zh-CN/docs/Web/API/CanvasGradient 渐变
            //线性渐变
            //两个点是需要渐变的地方
            const linearGradient = ctx.createLinearGradient(0,0,600,400);
            //0开始1结束,线性渐变从红色到蓝色
            linearGradient.addColorStop(0,'red')
            linearGradient.addColorStop(1,'blue')
            //让线条渐变
            ctx.strokeStyle = linearGradient;
            ctx.stroke()
        }, 2000);


    }

    const button = document.getElementById('bt');
    button.addEventListener('click',geneateCanvas)



</script>
</html>